package com.authine.cloudpivot.ext.utils.email;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.authine.cloudpivot.ext.constants.EmailConstant;
import com.authine.cloudpivot.ext.constants.SMSConstant;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MailUtis {
	// 发信域名
	static final String emailDomainName = EmailConstant.EMAIL_DOMAIN_NAME;
	// 发信地址
	static final String accountName = EmailConstant.ACCOUNT_NAME;
	// 发信人昵称
	static final String nickName = EmailConstant.NICK_NAME1;
	//短信API产品域名
	static final String accessKeyId= SMSConstant.ACCESS_KEY_ID;
	//短信API产品域名
	static final String accessKeySecret= SMSConstant.ACCESS_KEY_SECRET;
	static final String regionId= SMSConstant.REGION_ID;

	/**
	 * 发送邮件
	 * 
	 * @param toAddress 收件人
	 * @param subject   邮件主题
	 * @param message   邮件内容
	 * @param flag      1:邮件 text 正文，2:邮件 html 正文
	 * @return
	 */
	public static void sendEmail(String toAddress,String subject,String message,int flag) {
		
		if(toAddress==null || "".equals(toAddress))throw new RuntimeException("收件人地址不能为空");
		if(subject==null || "".equals(subject)) {
			throw new RuntimeException("邮件主题不能为空");
		}else if(subject.length()>100){
			throw new RuntimeException("主题错误，主题不能超过100个字符");
		}
		if(message==null || "".equals(message))throw new RuntimeException("邮件正文不能为空");
		
		SingleSendMailRequest request = new SingleSendMailRequest();
		try {
			request.setAccountName(accountName);
			request.setAddressType(1);
			request.setReplyToAddress(true);
			request.setToAddress(toAddress);
			request.setSubject(subject);
			request.setFromAlias(nickName);
			if(flag==1) {
				request.setTextBody(message);
			}else if(flag==2) {
				request.setHtmlBody(message);
			}else {
				throw new RuntimeException("请选择正确的邮件正文类型");
			}
			IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
			IAcsClient client = new DefaultAcsClient(profile);
			SingleSendMailResponse httpResponse = client.getAcsResponse(request);
			log.info("----------------------请求ID----------------------"+httpResponse.getRequestId());
			log.info("----------------------事件ID----------------------"+httpResponse.getEnvId());
		} catch (ServerException e) {
			e.printStackTrace();
		}catch (ClientException e) {
			if("InvalidReceiverName".equals(e.getErrCode()))throw new RuntimeException("收件人格式不正确");
			if("InvalidTemplateName".equals(e.getErrCode()))throw new RuntimeException("模板名格式不正确");
			if("InvalidMailAddress".equals(e.getErrCode()))throw new RuntimeException("发信地址不存在");
			if("InvalidMailAddress".equals(e.getErrCode()))throw new RuntimeException("指定的模板不存在");
			
			if("InvalidReceiver".equals(e.getErrCode()))throw new RuntimeException("收件人不存在");
			if("InvalidToAddress".equals(e.getErrCode()))throw new RuntimeException("收件人地址错误，请检查");
			if("InvalidBody".equals(e.getErrCode()))throw new RuntimeException("textBody或textBody格式错误");
			if("InvalidSubject".equals(e.getErrCode()))throw new RuntimeException("主题错误，主题不能超过100个字符");
		}
	}
	
	public static void main(String[] args) {
		sendEmail("229145171@qq.com","测试主题","<a href='http://www.baidu.com'>测试</a>1",2);
		
	}

}
